package com.example.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.entity.Post;
import com.example.vo.CommentVo;
import com.example.vo.PostVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @author fangyu
 * @version v1.0.0
 * @date 2020/7/20 9:40 下午
 */
@Controller
public class PostController extends BaseController{

    /**
     *
     * @param id 文章id \\d* 指定参数类型
     * @return
     */
    @GetMapping("/category/{id:\\d*}")
    public String category(@PathVariable(name = "id") Long id) {
        int page = ServletRequestUtils.getIntParameter(request, "page", 1);
        request.setAttribute("currentCategoryId", id);
        request.setAttribute("page", page);
        return "post/category";
    }

    @GetMapping("/post/{id:\\d*}")
    public String detail(@PathVariable(name = "id") Long id) {
        PostVo vo = postService.selectOnePost(new QueryWrapper<Post>().eq("p.id", id));
        Assert.notNull(vo, "文章已被删除");

        // 1分页 2文章id 3用户id 4排序
        IPage<CommentVo> result = commentService.paging(getPage(), vo.getId(), null, "created");
        request.setAttribute("currentCategoryId", vo.getCategoryId());
        request.setAttribute("post", vo);
        request.setAttribute("pageData", result);
        return "post/detail";
    }
}
